/**
 * Project Name:gc.sms.mvn
 * File Name:PositionController.java
 * Package Name:com.clps.dev.sms.controller.sm
 * Date:2018-12-03 14:57:10
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.controller.sm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultState;
import com.clps.dev.sms.entity.sm.Position;
import com.clps.dev.sms.service.sm.IPositionService;
import com.clps.dev.sms.util.factory.ToolFactory;
import com.clps.dev.sms.util.page.PageBean;

/**
 * ClassName: PositionController <br/>
 * Description: Position控制层
 *
 * @author Jack.Huang
 * @version V1.0
 * @since JDK 1.8
 * @date:2018-12-03 14:57:10
 */
@Controller
@RequestMapping(value = "/pos")
public class PositionController {
	@Resource
	private IPositionService positionService;
	Logger logger = Logger.getLogger(PositionController.class);
	List<Position> employees = null;
	PageBean<Position> pageBean = new PageBean<>();
	String name = "positionName";

	@GetMapping(value = "/add")
	public String addPosition() {
		return "sm/position_add";
	}

	@PostMapping(value = "/pos/add")
	@ResponseBody
	public ResponseResult<Position> createPosition(@RequestBody Position position) {
		position.setPositionCreateTime(ToolFactory.getDateInstance(CommonData.YYYY_MM_DD_HH_MM_SS));
		position.setPositionCreateName(1);
		position.setPositionUpdatedEmployeeName(1);
		ResponseResult<Position> responseResult = positionService.addPosition(position);
		return responseResult;
	}

	@GetMapping(value = "/list")
	public String showPosition() {
		return "sm/position_list";
	}

	@GetMapping(value = "/pos")
	@ResponseBody
	public ResponseResult<Position> getPosition() {
		ResponseResult<Position> responseResult = positionService.queryAllPosition();
		Map<String, Object> positionList = new HashMap<>();
		@SuppressWarnings("unchecked")
		List<Position> positions = (List<Position>) responseResult.getData();
		positionList.put("positions", positions);
		responseResult.setData(positionList);
		responseResult.setPageInfo(null);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}

	@GetMapping(value = "/pos/{positionId}")
	public String editPosition(@PathVariable("positionId") String positionId, Model model) {
		int id = Integer.parseInt(positionId);
		ResponseResult<Position> responseResult = positionService.queryPositionById(id);
		Position position = (Position) responseResult.getData();
		model.addAttribute("position", position);
		return "sm/position_edit";
	}

	@PutMapping(value = "/pos/edit")
	@ResponseBody
	public ResponseResult<Position> updatePosition(@RequestBody Position position) {
		ResponseResult<Position> result = positionService.editPosition(position);
		return result;
	}

	@GetMapping(value = "/pos/all")
	@ResponseBody
	public ResponseResult<Position> showAllPosition() {
		ResponseResult<Position> responseResult = positionService.queryAllPosition();
		Map<String, Object> positionList = new HashMap<>();
		@SuppressWarnings("unchecked")
		List<Position> positions = (List<Position>) responseResult.getData();
		positionList.put("positions", positions);
		responseResult.setData(positionList);
		responseResult.setPageInfo(null);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}

}
